//分页
  var pageSize=6;//每页要显示的条数
  var currentPage =1;//当前为第一页
  var isLoading = false;//是否加载更多的数据
$(function(){
	//重置 数据只是清空的操作
	$('#btn-reset').click(function(){
		$('#search-userName').val("");
	});
	//查询所有用户信息
	selectAllUserInfo(1);
	function selectAllUserInfo(pageNum){
		  $('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>用户名</td></tr>');
      var p={};
      p.pageSize = pageSize;
      p.pageNum = pageNum;
		  $.ajax({
		  type:"POST",
		  url:"/movie.front/adminUserController/findAllAdminList",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result.list;
           $('#user-order').empty();
           $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>用户名</td></tr>');
           for(var i=0;i<array.length;i++){
        	   var html = '' ;
        		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].adminUserName+'</td></tr>';
        		   $('#user-order').append(html); 
           }
           //总共多少页
              var totalPages = data.result.pages;
              $('#paginator').empty();
        	   var pageHtml = '<ul>';
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
        	   for(var i=0;i<totalPages;i++){
        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
        	   }
        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
        	   pageHtml =pageHtml+'</ul>';
        	   $('#paginator').append(pageHtml);
		   
		  }
	
	   });
		
	}
	//查询信息
	$('#btn-search').click(function(){
    var userName =	$('#search-userName')[0].value;
/*	 var p = {userName:userName,mail:mail};
	 selectAllUserInfo(p);*/
	querySearchUser(1,userName);
	});
	
	function querySearchUser(pageNum,userName){
		var p ={};
		p.pageNum = pageNum;
		p.pageSize = pageSize;
		p.userName = userName;
		  $.ajax({
			  type:"POST",
			  url:"/movie.front/adminUserController/findAllAdminList",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
	           array = data.result.list;
	           $('#user-order').empty();
	           $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>用户名</td></tr>');
	           for(var i=0;i<array.length;i++){
	        	   var html = '' ;
	        	
	        		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].adminUserName+'</td></tr>';
	        		   $('#user-order').append(html); 
	        	   
	           }
	           //总共多少页
	              var totalPages = data.result.pages;
	              $('#paginator').empty();
	        	   var pageHtml = '<ul>';
	        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
	        	   for(var i=0;i<totalPages;i++){
	        		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
	        	   }
	        	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
	        	   pageHtml =pageHtml+'</ul>';
	        	   $('#paginator').append(pageHtml);
			   
			  }
		
		   });
	}
	
	$('#btn-insert').click(function(){
		$('#userName').val("");
		$('#myModal').modal('show');
		/*alert('aaaaaaa');*/
	})
	//添加用户信息
	$('#btn-save-user').click(function(){
	var userName =	$('#userName')[0].value;
	var password =	$('#password')[0].value;
	var id = $('#id')[0].value;
	//校验数据
	if(userName==undefined || userName=="" ){
		return false;
	}

	if(id==null || id==""){
		var p={userName:userName,password:password};
	$.ajax({
		  type:"POST",
		  url:"/movie.front/adminUserController/addAdminUser",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
            if(data.successResultInfo!=null){
            	//关闭对话框
            	$('#myModal').modal('hide');
            	//刷新表格
            	selectAllUserInfo(1);
            }
            if(data.errorResultInfo!=null){
            	alert(data.errorResultInfo.errorcode);
            }
		   
		  }
	
	   });
	}else{
		var p={id:id,userName:userName,email:email};
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/updateUser",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  
					  $('#myModal').modal('hide');
						//刷新表格
					  selectAllUserInfo(1);					  
				  

			   
			  }
		
		   });
	 }
	});
	
	//修改用户信息
	$('#btn-update-user').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  $('#myModal').modal('show');
			  var id = {id:id};
			  getByIdUserInfo(id);
		     }
	});
	
	//删除用户信息
	$('#btn-remove-user').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	 var id = {id:id};
		    	 deleteUserInfo(id);
		      }
		
	});
	function deleteUserInfo(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/deleteUser",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  alert("删除成功");
				  selectAllUserInfo(1);
				  
			     }
		   });
	}
	function getByIdUserInfo(p){
		//回显信息
	
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/getUserById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  $('#id')[0].value=data.result.id;
	           $('#userName')[0].value=data.result.userName;
	           $('#email')[0].value=data.result.email;
			   
			  }
		
		   });
	}
	
	//修改密码
	$('#btn-remove-password').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	
		    	 updatePassword(id);
		      }
	});
	
	function updatePassword(p){
		var userId = p;
	
		$('#myPasswordModal').modal('show');
		$('#btn-save-password').click(function(){
			var newPassword = $('#newPassword')[0].value;
			var surePassword =$('#surePassword')[0].value;
			if(newPassword!=surePassword){
				alert("两次输入的密码不一致，请重新输入");
			}else{
				var id = userId;
				 var p = {id:id,newPassword:newPassword};
				updatePasswordById(p)
			}
		});
	}
	function updatePasswordById(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/userInfo/updatePasswordById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				 var result = data.success;
			     if(result>0){
			    	 $('#myPasswordModal').modal('hide');
			    	 selectAllUserInfo(1);
			     }
			  }
		   });
	}
	
 });
//当前页
function currentPagesquery(currentId){
	 // alert(currentId);当前页码
	  $('#paginator').empty();
	     var p ={};
         p.pageSize = 6;
         p.pageNum = currentId;
         $('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>用户名</td></tr>');
    $.ajax({
	  type:"POST",
	  url:"/movie.front/userInfo/findAllUsersList",
	  data:JSON.stringify(p),
	  dataType:"json",
	  contentType:"application/json",
	  success:function(data){
		  array = data.result.list;
		  if(currentId==1){
          for(var i=0;i<array.length;i++){
       	   var html = '' ;
       	  
       		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].adminUserName+'</td></tr>';
          	    $('#user-order').append(html); 
       	  
          }
		 }else{
			    for(var i=0;i<array.length;i++){
			    	 var flag = (currentId-1)*6+i+1;
			       	   var html = '' ;
			       		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+flag+'</td><td>'+array[i].adminUserName+'</td></tr>';
			          	    $('#user-order').append(html); 
			       	 
			       	   
			          } 
		 }
          //总共多少页
             var totalPages = data.result.pages;
             $('#paginator').empty();
       	   var pageHtml = '<ul>';
       	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="firstPage" onclick="firstPagesquery(1)"> 首页 </a></li>';
       	   for(var i=0;i<totalPages;i++){
       		  pageHtml = pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" onclick="currentPagesquery('+(i+1)+')" id='+(i+1)+'>'+(i+1)+'</a></li>'; 
       	   }
       	   pageHtml =pageHtml+'<li class="m-pagination-page"><a herf="javascript:void(0);" id="endPage" onclick="endPagequery('+totalPages+')">尾页</a></li>';
       	   pageHtml =pageHtml+'</ul>';
       	   $('#paginator').append(pageHtml);
	  }

  });
}


//首页
function  firstPagesquery(firstPageId){
	currentPagesquery(firstPageId);
}

//尾页
function  endPagequery(endPageId){
	currentPagesquery(endPageId);
	 }